package com.robot.controller;

import com.robot.service.LangChainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/llm")
public class LLMController {

    private static final Logger logger = LoggerFactory.getLogger(LLMController.class);

    @Autowired
    private LangChainService langChainService;

    @PostMapping("/process")
    public Map<String, Object> processNaturalLanguage(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        String query = request.get("query");
        
        if (query == null || query.trim().isEmpty()) {
            response.put("success", false);
            response.put("message", "查询内容不能为空");
            return response;
        }

        try {
            String result = langChainService.processQuery(query);
            response.put("success", true);
            response.put("result", result);
        } catch (Exception e) {
            logger.error("处理查询失败", e);
            response.put("success", false);
            response.put("message", "处理请求时出现问题，请稍后再试");
        }

        return response;
    }

    @PostMapping("/chat")
    public Map<String, Object> chat(@RequestBody Map<String, String> request) {
        Map<String, Object> response = new HashMap<>();
        String query = request.get("query");
        
        if (query == null || query.trim().isEmpty()) {
            response.put("success", false);
            response.put("message", "查询内容不能为空");
            return response;
        }

        try {
            String result = langChainService.chat(query);
            response.put("success", true);
            response.put("result", result);
        } catch (Exception e) {
            logger.error("处理聊天失败", e);
            response.put("success", false);
            response.put("message", "聊天处理失败，请稍后再试");
        }

        return response;
    }

    @GetMapping("/available-apis")
    public Map<String, Object> getAvailableApis() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 获取真实的API列表
            Map<String, Object> apis = new HashMap<>();
            apis.put("fireAlarm", "获取火警数据 - GET /api/fire-alarm/count - 参数: company, year, month");
            apis.put("maintenance", "获取维保状态 - GET /api/maintenance/status - 参数: company, year, month");
            
            response.put("success", true);
            response.put("apis", apis);
            logger.info("成功返回可用API列表");
        } catch (Exception e) {
            logger.error("获取API列表时发生错误", e);
            response.put("success", false);
            response.put("message", "获取API列表失败: " + e.getMessage());
        }
        
        return response;
    }
    
    @GetMapping("/test")
    public Map<String, Object> testConnection() {
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "LLM API服务连接正常");
        logger.info("LLM API测试接口被调用");
        return response;
    }
} 